<?php

namespace App\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Site;
use ModuleService;
use Spatie\Permission\Models\Role;

class PermissionController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth:sanctum']);
    }

    public function site(Site $site)
    {
        $modules = ModuleService::getSiteModules($site);
        $modules->map(function ($module) {
            $module['permissions'] = ModuleService::config($module['name'], 'permissions');
        });
        return $modules;
    }

    public function role(Request $request, Site $site, Role $role)
    {
        $role->syncPermissions($request->permissions);
        return $this->message('角色权限设置成功');
    }
}
